package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealDishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.InterfaceAddress;
import java.util.List;

@Service
public class SetmealDishServiceImpl implements SetmealDishService {


    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);

        //套餐表中插入套餐基本信息
        setmealDishMapper.save(setmeal);

        Long id = setmeal.getId();

        //往套餐菜品表中插入信息
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if (list.size() > 0 && list != null) {
            for (SetmealDish setmealDish : list) {
                setmealDish.setSetmealId(id);
                setmealDishMapper.insert(setmealDish);
            }
        }

    }

    @Override
    public List<Dish> addDish(Long id) {
        Dish dish = Dish.builder()
                .categoryId(id)
                .status(StatusConstant.ENABLE)
                .build();
        return setmealDishMapper.addDish(dish.getId());
    }

    @Override
    public PageResult queryStmealByPage(SetmealPageQueryDTO setmealDTO) {

        //使用pagehelper插件 开启分页查询
        PageHelper.startPage(setmealDTO.getPage(), setmealDTO.getPageSize());


        //强制转换为Page对象
        Page<SetmealVO> page = setmealDishMapper.querySetmaelByPage(setmealDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public SetmealVO setmealById(Long id) {

        //根据id获取到对应的套餐
        SetmealVO setmealVO = setmealDishMapper.setmealById(id);

        return setmealVO;

    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);

        //更改套餐表
        setmealDishMapper.update(setmeal);

        List<SetmealDish> list = setmealDTO.getSetmealDishes();





        if (list.size() > 0 && list != null) {
            list.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            //删除原有套餐所关联的菜品
            setmealDishMapper.delete(setmealDTO.getId());
        }

        //插入更新的菜品
        setmealDishMapper.add(list);

    }

    @Override
    public void deleteByIds(List<Long> ids) {

        ///判断套餐是否在售卖状态 只有停售时才能删除
        for (Long id : ids) {
           Integer status = setmealDishMapper.selectStatus(id);
           if(status==0){
               //删除套餐菜品表
               setmealDishMapper.deletesetmeal_dishByIds(id);
           }
        }
        //删除套餐表
        setmealDishMapper.deleteByIds(ids);

    }

    @Override
    public void startOrstop(Long setmealId) {

      Integer status = setmealDishMapper.getStatusById(setmealId);

//      if(status1==0){
//          Integer status2 = 1;
//          //更改套餐状态
//          setmealDishMapper.startOrstop(status2);
//      }
//      else if (status1==1) {
//          Integer status2 = 0;
//          //更改套餐状态
//          setmealDishMapper.startOrstop(status2);
//      }

        //change等于1说明是起售操作 0则为停售
      Integer change = (status==0) ? 1: 0;


        //起售前判断起售套餐中是否有已经停售的菜品 如果有就报错
        if (change == 1) {
            List<Long> list = setmealDishMapper.getAllDishId(setmealId);
            for (Long dishId : list) {
                Integer dishStatus = dishMapper.lookStatus();
                if (status == 0){
                    //改套餐中包含已经被停售的菜品 无法起售
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }

        setmealDishMapper.startOrstop(change,setmealId);

    }

    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealDishMapper.list(setmeal);
        return list;
    }

    public List<DishItemVO> getDishItemById(Long id) {
        return setmealDishMapper.getDishItemBySetmealId(id);
    }

}
